package gold.digger;

import gold.utils.InputUtil;

import java.util.Arrays;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC757 {
    public long startExecuteTime = System.currentTimeMillis();


    /*
     * @param 此题目参考了别人代码
     * 不是因为复杂，而是因为经典
     * 未来需要再次复习此道题目
     * @return:
     */
    class Solution {
        public int intersectionSizeTwo(int[][] intervals) {
            Arrays.sort(intervals, (o1, o2) -> o1[0] == o2[0] ? o2[1] - o1[1] : o1[0] - o2[0]);
            int n = intervals.length;
            //初始的两个元素
            int cur = intervals[n - 1][0];
            int next = intervals[n - 1][0] + 1;
            int ans = 2;
            //从后向前遍历
            for (int i = n - 2; i >= 0; i--) {
                //开始分类讨论
                if (intervals[i][1] >= next) {
                    continue;
                } else if (intervals[i][1] < cur) {
                    cur = intervals[i][0];
                    next = intervals[i][0] + 1;
                    ans = ans + 2;
                } else {
                    next = cur;
                    cur = intervals[i][0];
                    ans++;
                }
            }
            return ans;
        }
    }
    

    public void run() {
        int[] arr = InputUtil.toIntegerArray("[1,2,3]");
        System.out.println(new Solution().toString());
    }

    public static void main(String[] args) throws Exception {
        LC757 an = new LC757();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
